package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.inter.GetAgeInter;
import com.dlwx.parttimetalent.utils.DateUtil;
import com.flyco.dialog.widget.base.BaseDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/21.
 */

public class ClockDialog extends BaseDialog {
    @BindView(R.id.time_picker)
    TimePicker datepicker;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    private Context context;
    private GetAgeInter inter;
    String[] minuts = new String[]{"00","10", "20", "30","40","50"};//间隔15的数组，用来表示可设置的分钟值


    public ClockDialog(Context context, GetAgeInter inter) {
        super(context);
        this.context = context;
        this.inter=inter;


    }

    @Override
    public View onCreateView() {
        widthScale(0.9f);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_clock, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforShow() {
        datepicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        datepicker.setIs24HourView(true);
        setNumberPickerTextSize(datepicker);
        long currentTime=DateUtil.getSystemTime();
        long changeTime=currentTime+(1000*60*40);
        int hour=0 ,minute=0;
        String str=DateUtil.getStandardTimeByhm(changeTime);
        String[] startTimes=str.split(":");
        if(startTimes.length>1){
            hour=Integer.valueOf(startTimes[0]);
            minute=Integer.valueOf(startTimes[1]);
        }
        int m=minute/10;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            datepicker.setHour(hour);
            datepicker.setMinute(m);
        }else{
            datepicker.setCurrentHour(hour);
            datepicker.setCurrentMinute(m);
        }
    }

    @OnClick({R.id.tv_confirm, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                inter.getAge(datepicker.getCurrentHour(), datepicker.getCurrentMinute()*10,0,"");
                this.dismiss();
                break;
            case R.id.tv_cancel:
                this.dismiss();
                break;
        }
    }


    /**
     * 得到timePicker里面的android.widget.NumberPicker组件 （有两个android.widget.NumberPicker组件--hour，minute）
     * @param viewGroup
     * @return
     */
    private List<NumberPicker> findNumberPicker(ViewGroup viewGroup)
    {
        List<NumberPicker> npList = new ArrayList<NumberPicker>();
        View child = null;

        if (null != viewGroup)
        {
            for (int i = 0; i < viewGroup.getChildCount(); i++)
            {
                child = viewGroup.getChildAt(i);
                if (child instanceof NumberPicker)
                {
                    npList.add((NumberPicker)child);
                }
                else if (child instanceof LinearLayout)
                {
                    List<NumberPicker> result = findNumberPicker((ViewGroup)child);
                    if (result.size() > 0)
                    {
                        return result;
                    }
                }
            }
        }

        return npList;
    }

    /**
     * 查找timePicker里面的android.widget.NumberPicker组件 ，并对其进行时间间隔设置
     * @param viewGroup  TimePicker timePicker
     */
    private void setNumberPickerTextSize(ViewGroup viewGroup){
        List<NumberPicker> npList = findNumberPicker(viewGroup);
        if (null != npList)
        {
            for (NumberPicker mMinuteSpinner : npList)
            {
//              System.out.println("mMinuteSpinner.toString()="+mMinuteSpinner.toString());
                if(mMinuteSpinner.toString().contains("id/minute")){//对分钟进行间隔设置

                    mMinuteSpinner.setMinValue(0);
                    mMinuteSpinner.setMaxValue(minuts.length-1);
                    mMinuteSpinner.setDisplayedValues(minuts);  //这里的minuts是一个String数组，就是要显示的分钟值
                }
                //对小时进行间隔设置 使用 if(mMinuteSpinner.toString().contains("id/hour")){}即可
            }
        }
    }
}
